package com.example.musicproject.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.Acticledto;
import com.example.common.dto.ArticleInfo;
import com.example.common.dto.CommentParam;
import com.example.common.dto.Commentdto;
import com.example.common.pojo.musictag;
import com.example.common.utils.R;
import com.example.service.MusicArticleservice;
import com.example.service.MusicCommentservice;
import com.example.service.MusicTagservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/article")
@Api(tags = "文章管理",value = "文章管理")
public class ArticleController {

    @Autowired
    private MusicArticleservice musicArticleservice;

    @Autowired
    private MusicCommentservice musicCommentservice;

    @Autowired
    private MusicTagservice musicTagservice;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/articleList")
    @ApiOperation(value = "文章列表", notes = "文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "int", paramType = "query",example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query",example = "10"),
            @ApiImplicitParam(name = "name", value = "文章名", required = false, dataType = "String", paramType = "query",example = "音乐"),
            @ApiImplicitParam(name = "tagid", value = "标签id", required = false, dataType = "Long", paramType = "query",example = "1"),
            @ApiImplicitParam(name = "order", value = "排序",required = false, dataType = "int", paramType = "query",example = "1")
    })
    public R<Page<Acticledto>> articlelist(int page, int pageSize, String name , Long tagid, Integer order){
        Page<Acticledto> page1 = musicArticleservice.selectPagearticle(page, pageSize,name,tagid);
        System.out.println("================"+order);
        for (Acticledto a: page1.getRecords()
             ) {
            Double isperfectscore = stringRedisTemplate.boundZSetOps("isperfect").score(String.valueOf(a.getId()));
            Double perfectscore = stringRedisTemplate.boundZSetOps("perfect").score(String.valueOf(a.getId()));
            Double viewscore = stringRedisTemplate.boundZSetOps("view").score(String.valueOf(a.getId()));
            a.setIsperfect((int)Math.round(isperfectscore));
            a.setPerfect((int)Math.round(perfectscore));
            a.setViewCounts((int)Math.round(viewscore));
        }
        return R.success(page1);
    }

    @GetMapping("/articleinfo/{articleId}")
    @ApiOperation(value = "文章详情", notes = "文章详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章id", required = true, dataType = "Long", paramType = "path",example = "1")
    })
    public R<ArticleInfo> articleInfo(@PathVariable("articleId") Long articleId){
        ArticleInfo articleInfo = musicArticleservice.selectArticleInfo(articleId);
        return  R.success(articleInfo);
    }

    @GetMapping("/comment/{commentId}")
    @ApiOperation(value = "评论列表", notes = "评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论id", required = true, dataType = "Long", paramType = "path",example="1")
    })
    public List<Commentdto> commentList(@PathVariable("commentId") Long commentId){
        List<Commentdto> commentlist = musicCommentservice.commentlist(commentId);
        return commentlist;
    }

    @PostMapping("/commentadd")
    @ApiOperation(value = "评论添加", notes = "评论添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header",example = "token")
            ,@ApiImplicitParam(name = "commentParam", value = "评论参数", required = true, dataType = "CommentParam", paramType = "body",example = "commentParam")
    })
    public R<String> commentadd(@RequestBody CommentParam commentParam, @RequestHeader("token") String token){
        boolean commentadd = musicCommentservice.commentadd(commentParam, token);
        if (commentadd){
            return R.success("评论成功!");
        }
        return R.error("评论失败，请联系管理员!");
    }

    @PostMapping("/taglist")
    @ApiOperation(value = "标签列表", notes = "标签列表")
    public R<List<musictag>> taglist(){
        List<musictag> list = musicTagservice.list();
        return R.success(list);
    }

}
